﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using IUAP;
using System.Security;
using CKEditor.NET;

public partial class PagAdmin_AddEditArticulos : BasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            // if a ID param is present on the querystring, switch to Edit mode for that article,
            // but only after checking that the current user is an Administrator or an Editor
            if (!string.IsNullOrEmpty(this.Request.QueryString["ID"]))
            {
                if (this.User.Identity.IsAuthenticated &&
                   (this.User.IsInRole("Administrador") || this.User.IsInRole("Moderador")))
                {
                    dvwArticle.ChangeMode(DetailsViewMode.Edit);
                    UpdateTitle();
                }
                else
                    throw new SecurityException("Usted no tiene permiso para editar artículos existentes!");
            }
        }
    }
    protected void dvwArticle_ItemCreated(object sender, EventArgs e)
    {
        Control ctl = dvwArticle.FindControl("txtBody");
        if (ctl != null)
        {
            CKEditorControl txtBody = ctl as CKEditorControl;
            txtBody.BasePath = this.BaseUrl + "CKEditorControl/";
        }
    }
    protected void dvwArticle_DataBound(object sender, EventArgs e)
    {
        // Tn InserMode, preselect the checkboxes to make the article listed and to allow comments
        // The Approved checkbox is selected and enabled only if the user belongs to the
        // Administrators or Editors group instead.
        if (dvwArticle.CurrentMode == DetailsViewMode.Insert)
        {
            CheckBox chkApproved = dvwArticle.FindControl("chkApproved") as CheckBox;
            CheckBox chkListed = dvwArticle.FindControl("chkListed") as CheckBox;
            CheckBox chkCommentsEnabled = dvwArticle.FindControl("chkCommentsEnabled") as CheckBox;

            chkListed.Checked = true;
            chkCommentsEnabled.Checked = true;

            bool canApprove = (this.User.IsInRole("Administrador") || this.User.IsInRole("Moderador"));
            chkApproved.Enabled = canApprove;
            chkApproved.Checked = canApprove;
        }
    }
    private void UpdateTitle()
    {
        lblNewArticle.Visible = (dvwArticle.CurrentMode == DetailsViewMode.Insert);
        lblEditArticle.Visible = !lblNewArticle.Visible;
    }
    protected void dvwArticle_ModeChanged(object sender, EventArgs e)
    {
        UpdateTitle();
    }
}